using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(MenuDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:PostControlDataSource runat=server/>")]
    public class MenuDataSource : GenericDataSource<Bll.Menu>
    {
        public enum Fields
        {
            Id,
            SiteId,
            ConcurencyId,
            Name,
            ParentPath,
            IncludeParent,
            DefaultForAllPosts,
            Notes
        }

        protected override Acms.Bll.Menu PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Acms.Bll.Menu tmp = null;
            byte[] concurencyId = null;
            if (action == UpdateAction.Insert)
            {
                tmp = Acms.Bll.Menu.CreateInstance();
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                int siteId = GetValue<int>(Fields.SiteId, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = Acms.Bll.Menu.GetById(id, siteId);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);
                if(RequiresUpdate<string>(Fields.Name, values, oldValues))
                {
                    tmp.Name = GetValue<string>(Fields.Name, values);
                }
                if (RequiresUpdate<string>(Fields.ParentPath, values, oldValues))
                {
                    tmp.ParentPath = GetValue<string>(Fields.ParentPath, values);
                }
                if (RequiresUpdate<bool>(Fields.IncludeParent, values, oldValues))
                {
                    tmp.IncludeParent = GetValue<bool>(Fields.IncludeParent, values);
                }
                if (RequiresUpdate<bool>(Fields.DefaultForAllPosts, values, oldValues))
                {
                    tmp.DefaultForAllPosts = GetValue<bool>(Fields.DefaultForAllPosts, values);
                }
                if(RequiresUpdate<string>(Fields.Notes, values, oldValues))
                {
                    tmp.Notes = GetValue<string>(Fields.Notes, values);
                }
            }
            return tmp;
        }

        protected override int SaveChanges(Acms.Bll.Menu item)
        {
            item.Update();
            return 1;
        }

        protected override int Delete(Acms.Bll.Menu item)
        {
            item.Delete();
            return 1;
        }

        protected override void AddInsertedKey(Acms.Bll.Menu item, System.Collections.IDictionary values)
        {
            values[Fields.Id.ToString()] = item.Id;
        }
    }

    public class MenuDataSourceDesigner : GenericDataSourceDesigner<Bll.Menu>
    {
        protected override Acms.Bll.Menu GetDesignTimeItem(int index)
        {
            string[] names = new string[] { "Main", "Footer", "PageNavigation"};
            Random rnd = new Random(index);
            Bll.Menu tmp = Bll.Menu.CreateInstance();
            tmp.Name = names[rnd.Next(0, names.Length)];
            tmp.DefaultForAllPosts = rnd.Next(0, 1) == 1;
            tmp.IncludeParent = rnd.Next(0, 1) == 1;
            return tmp;
        }
    }
}
